package JDBC;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 完成用户的注册功能
 * 程序启动后，要求用户陆续输入用户名，密码，昵称，年龄
 * 然后将该信息插入到userinfo表中完成注册
 */
public class RegDemo {
    public static void main(String[] args) {
        // 获取数据库连接
        try (Connection connection = DBUtil.getConnection()){
            //创建Statement对象
            Statement statement = connection.createStatement();
            //创建userinfo表，表里有用户名，密码，昵称，年龄
            String sql = "CREATE TABLE userinfo(" +
                    "id INT PRIMARY KEY AUTO_INCREMENT, " +
                    "username VARCHAR(20) NOT NULL, " +
                    "password VARCHAR(20) NOT NULL, " +
                    "nickname VARCHAR(20) NOT NULL, " +
                    "age INT(3) NOT NULL)";
            statement.execute(sql);
            System.out.println("创建成功");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            System.out.println("请输入昵称：");
            String nickname = scanner.nextLine();
            System.out.println("请输入年龄：");
            int age = scanner.nextInt();
            String sql2 = "INSERT INTO userinfo(username, password, nickname, age) " +
                    "VALUES('"+username+"', '"+password+"', '"+nickname+"', "+age+")";
            int i = statement.executeUpdate(sql2);
            if (i>0){
                System.out.println("注册成功");
            }else {
                System.out.println("注册失败");
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
